package com.dongdongshop.imp;

import com.dongdongshop.mapper.TbAddressMapper;
import com.dongdongshop.mapper.TbAreasMapper;
import com.dongdongshop.mapper.TbCitiesMapper;
import com.dongdongshop.mapper.TbProvincesMapper;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.TbAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@com.alibaba.dubbo.config.annotation.Service
public class TbAddressServiceImp implements TbAddressService {
    @Autowired
    private TbAddressMapper tbAddressMapper;
    @Autowired
    private TbProvincesMapper tbProvincesMapper;
    @Autowired
    private TbCitiesMapper tbCitiesMapper;
    @Autowired
    private TbAreasMapper tbAreasMapper;

    @Override
    public List<TbAddress> getOrderByUserId(String username) {
        TbAddressExample tbAddressExample = new TbAddressExample();
        tbAddressExample.createCriteria().andUserIdEqualTo(username);
        List<TbAddress> tbAddresses = tbAddressMapper.selectByExample(tbAddressExample);
        return tbAddresses;
    }

    @Override
    public TbAddress getAddress(Long id) {
        return tbAddressMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<TbAddress> selectAddrssAll(String username) {
        TbAddressExample ae = new TbAddressExample();
        ae.createCriteria().andUserIdEqualTo(username);
        List<TbAddress> tbAddresses = tbAddressMapper.selectByExample(ae);
        for (TbAddress address:tbAddresses) {
            String province = tbProvincesMapper.selectProvinceId(address.getProvinceId());
            address.setProvinceId(province);
            String city = tbCitiesMapper.selectCityId( address.getCityId());
            address.setCityId(city);
            String townId = tbAreasMapper.selectTownId( address.getTownId());
            address.setTownId(townId);
        }
        return tbAddresses;
    }

    @Override
    public List<TbProvinces> selectAllProvince() {
        return tbProvincesMapper.selectByExample(null);
    }

    @Override
    public String selectProvinceid(int id) {
        TbProvinces tbProvinces = tbProvincesMapper.selectByPrimaryKey(id);
        return tbProvinces.getProvinceid();
    }

    @Override
    public List<TbCities> showCity(String provinceid) {
        TbCitiesExample tc = new TbCitiesExample();
        tc.createCriteria().andProvinceidEqualTo(provinceid);
        List<TbCities> citiesList = tbCitiesMapper.selectByExample(tc);
        return citiesList;
    }

    @Override
    public String selectCityid(int id) {
        TbCities tbCities = tbCitiesMapper.selectByPrimaryKey(id);
        return tbCities.getCityid();
    }

    @Override
    public List<TbAreas> showTown(String cityid) {
        TbAreasExample ta = new TbAreasExample();
        ta.createCriteria().andCityidEqualTo(cityid);
        List<TbAreas> tbAreas = tbAreasMapper.selectByExample(ta);
        return tbAreas;
    }

    @Override
    public String selectProvince(int id) {
        TbProvinces tbProvinces = tbProvincesMapper.selectByPrimaryKey(id);
        return tbProvinces.getProvinceid();
    }

    @Override
    public String selectCity(int id1) {
        TbCities tbCities = tbCitiesMapper.selectByPrimaryKey(id1);
        return tbCities.getCityid();
    }

    @Override
    public String selectAreas(int id2) {
        TbAreas tbAreas = tbAreasMapper.selectByPrimaryKey(id2);
        return tbAreas.getAreaid();
    }

    @Override
    public int addInfAdr(TbAddress tbAddress) {
        return tbAddressMapper.insertSelective(tbAddress);
    }
}
